package cn.cagurzhan.job;

import cn.cagurzhan.common.constant.CacheConstants;
import cn.cagurzhan.common.exception.ServiceException;
import cn.cagurzhan.domain.entity.HomeLog;
import cn.cagurzhan.domain.vo.SafetyVo;
import cn.cagurzhan.mapper.HomeLogMapper;
import cn.cagurzhan.service.CacheService;
import cn.cagurzhan.utils.BeanCopyUtils;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 同步任务，定时将Redis数据存入MySQL
 * @author AjaxZhan
 */
@Component
@RequiredArgsConstructor
public class HomeLogSyncJob {

    private final CacheService cacheService;

    private final HomeLogMapper homeLogMapper;

    /**
     * 同步Redis中的数据到MySQL
     * 频率：30s
     */
    // @Scheduled(fixedRate = 30 * 1000)
    // public void syncHomeLog(){
    //     // 从Redis中获取数据
    //     String json = cacheService.get(CacheConstants.HOME);
    //     SafetyVo safetyVo = JSON.parseObject(json, SafetyVo.class);
    //     HomeLog copy = BeanCopyUtils.copy(safetyVo, HomeLog.class);
    //     if(copy == null){
    //         throw new ServiceException("同步安全日志异常！");
    //     }
    //     // 保存到数据库
    //     copy.setLogId(null);
    //     copy.setLogTime(new Date());
    //     homeLogMapper.insert(copy);
    // }
}
